package com.spring.cloud.utils;

import com.spring.cloud.entity.UserInfo;
import org.junit.Before;
import org.junit.Test;

import java.security.PrivateKey;
import java.security.PublicKey;

public class TestToken {
    private static final String pubKeyPath = "D:\\miyao\\rsa.pub";

    private static final String priKeyPath = "D:\\miyao\\rsa.pri";

    private PublicKey publicKey;

    private PrivateKey privateKey;

    @Test
    public void testRsa() throws Exception {
        RsaUtils.generateKey(pubKeyPath, priKeyPath, "142536");
    }

    //测试生成密钥前要注释先，生成后在打开
    @Before
    public void testGetRsa() throws Exception {
        this.publicKey = RsaUtils.getPublicKey(pubKeyPath);
        this.privateKey = RsaUtils.getPrivateKey(priKeyPath);
    }

    @Test
    public void testGenerateToken() throws Exception {
        // 生成token
        String token = JwtUtils.generateToken(new UserInfo(12L, "hhdd"), privateKey, 5);
        System.out.println("token = " + token);
    }

    @Test
    public void testParseToken() throws Exception {
        String token = "eyJhbGciOiJSUzI1NiJ9.eyJpZCI6MTIsInVzZXJuYW1lIjoiaGhkZCIsImV4cCI6MTU1NDUxOTU1NX0.Llu9e_o-1hvo1pIdXVi_F7YU3DTgAIEh4-ElM0PSoFl4hQgch8DGOVFHyQbvCgaTx9kvU_1_jyMAHPL1njohROo-9yWBWWAzM-NGtWooq8I8K1YSJUAQQI6wlDJS8YocnhIG2rG9ABsoBhR08K0oREwwH3ijK_ggbgKzvOPo1bY";
        // 解析token
        UserInfo user = JwtUtils.getUserInfo( publicKey,token);
        System.out.println("id: " + user.getId());
        System.out.println("userName: " + user.getName());

    }
}
